package com.alex.wechat.mp.welcome.service.impl;

import java.io.Serializable;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alex.wechat.mp.config.WxContext;
import com.alex.wechat.mp.welcome.entity.WxWelcomeEntity;
import com.alex.wechat.mp.welcome.service.WxWelcomeServiceI;
import com.gomore.jeecg.common.Assert;

@Service("wxWelcomeService")
@Transactional
public class WxWelcomeServiceImpl extends CommonServiceImpl implements WxWelcomeServiceI {

  @Override
  public void delete(WxWelcomeEntity entity) {
    super.delete(entity);
    // 执行删除操作配置的sql增强
    this.doDelSql((WxWelcomeEntity) entity);
  }

  @Override
  public Serializable save(WxWelcomeEntity entity) {
    if (!WxContext.hasWxAccount()) {
      throw new RuntimeException("没有配置公众号");
    }

    WxWelcomeEntity subscribe = (WxWelcomeEntity) entity;
    subscribe.setAccountId(WxContext.getOriginalId());
    Serializable t = super.save(subscribe);
    // 执行新增操作配置的sql增强
    this.doAddSql((WxWelcomeEntity) entity);
    return t;
  }

  @Override
  public void saveOrUpdate(WxWelcomeEntity entity) {
    super.saveOrUpdate(entity);
    // 执行更新操作配置的sql增强
    this.doUpdateSql((WxWelcomeEntity) entity);
  }

  /**
   * 默认按钮-sql增强-新增操作
   * 
   * @param id
   * @return
   */
  public boolean doAddSql(WxWelcomeEntity t) {
    return true;
  }

  /**
   * 默认按钮-sql增强-更新操作
   * 
   * @param id
   * @return
   */
  public boolean doUpdateSql(WxWelcomeEntity t) {
    return true;
  }

  /**
   * 默认按钮-sql增强-删除操作
   * 
   * @param id
   * @return
   */
  public boolean doDelSql(WxWelcomeEntity t) {
    return true;
  }

  @Override
  @Transactional(readOnly = true)
  public WxWelcomeEntity getByAccountId(String accountId) {
    Assert.assertArgumentNotEmpty(accountId, "accountId");
    return findUniqueByProperty(WxWelcomeEntity.class, "accountId", accountId);
  }

  /**
   * 替换sql中的变量
   * 
   * @param sql
   * @return
   */
  // public String replaceVal(String sql,Subscribe t){
  // sql = sql.replace("#{id}",String.valueOf(t.getId()));
  // sql = sql.replace("#{accountname}",String.valueOf(t.getAccountname()));
  // sql = sql.replace("#{accounttoken}",String.valueOf(t.getAccounttoken()));
  // sql =
  // sql.replace("#{accountnumber}",String.valueOf(t.getAccountnumber()));
  // sql = sql.replace("#{accounttype}",String.valueOf(t.getAccounttype()));
  // sql = sql.replace("#{accountemail}",String.valueOf(t.getAccountemail()));
  // sql = sql.replace("#{accountdesc}",String.valueOf(t.getAccountdesc()));
  // sql = sql.replace("#{accountappid}",String.valueOf(t.getAccountappid()));
  // sql =
  // sql.replace("#{accountappsecret}",String.valueOf(t.getAccountappsecret()));
  // sql =
  // sql.replace("#{accountaccesstoken}",String.valueOf(t.getAccountaccesstoken()));
  // sql = sql.replace("#{UUID}",UUID.randomUUID().toString());
  // return sql;
  // }
}